package com.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.app.common.Result;
import com.app.model.ProjectModel;
import com.app.model.StatusModel;
import com.app.service.ProjectProgressService;
import com.app.util.StringTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 项目进度
 */
@Controller
@RestController
@RequestMapping("/api/project")
public class ProjectProgressController {
    @Autowired
    private ProjectProgressService projectProgressService;

    //查询
    @GetMapping("/type")
    public Result<Object> getByType(int page, int limit, int status, String name) {
        ProjectModel model = new ProjectModel();
        model.setPage((page - 1) * limit);
        model.setLimit(limit);
        model.setName(name);
        if (status == 1) {
            //查全部
            return Result.success(projectProgressService.projectList(model));
        }
        model.setStatus(status);
        //根据状态查
        return Result.success(projectProgressService.getListByType(model));
    }


    //重点项目后台管理
    //添加编辑重点项目
    @PostMapping("/insertAndUpdate")
    public Result<Object> insertAndUpdate(@RequestBody ProjectModel model) {
        if (model.getId() == 0) {
            model.setCreateTime(LocalDateTime.now());
            projectProgressService.insert(model);
        } else {
            model.setUpdateTime(LocalDateTime.now());
            projectProgressService.update(model);
        }
        return Result.success("操作成功");
    }


    //删除单位
    @GetMapping("/delete")
    public Result<Object> deleteProjectById(int id) {
        projectProgressService.deleteProjectById(id);
        return Result.success("操作成功！");
    }

    //状态更新
    @PostMapping("/updateStatus")
    public Result<Object> updateStatus(@RequestBody StatusModel model) {
        projectProgressService.updateStatus(model);
        return Result.success("操作成功！");
    }

    //查询列表
    //根据关键字查询
    @GetMapping("/getAll")
    public Result<Object> getAll(int page, int limit, String name, String year) {
        ProjectModel model = new ProjectModel();
        model.setPage((page - 1) * limit);
        model.setLimit(limit);
        model.setName(name);
        model.setYearName(year);
        return Result.success(projectProgressService.projectList(model));
    }

    //查询部门列表
    @GetMapping("/department")
    public Result<Object> getDepartmentList() {
        return Result.success(projectProgressService.getDepartmentList());
    }

    //状态列表
    @GetMapping("/getStatusList")
    public Result<Object> getStatusList() {
        return Result.success(projectProgressService.getStatusList());
    }

    //查看状态
    @GetMapping("/getStatus")
    public Result<Object> getStatus(Integer projectId) {
        return Result.success(projectProgressService.getStatus(projectId));
    }

    //项目类型查询
    @GetMapping("/getProjectClass")
    public Result<Object> getProjectClass() {
        return Result.success(projectProgressService.getProjectClass());
    }

    //类别查询  新建续建
    @GetMapping("/getProjectType")
    public Result<Object> getProjectType() {
        return Result.success(projectProgressService.getProjectType());
    }

    @GetMapping("/updateProject")
    public void updateProject() {
        projectProgressService.updateProject();
    }
}
